<?php
declare (strict_types = 1);

namespace app\admin\model\order;

use app\admin\model\ChargingStation;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class YkcOrder extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('id',$id)->find();
    }

    /**
     * 获取列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit){
        $sql = $this->where('status','>=',1);
        if(isset($getData['id']) && $getData['id']) $sql = $sql->where('id',$getData['id']);
        if(isset($getData['uid']) && $getData['uid']) $sql = $sql->where('uid',$getData['uid']);
        if(isset($getData['station_id']) && $getData['station_id']) $sql = $sql->where('station_id',$getData['station_id']);
        if(isset($getData['status']) && $getData['status']) $sql = $sql->where('status',$getData['status']);
        if(isset($getData['start_time']) && $getData['start_time']) $sql = $sql->where('start_time','>=',$getData['start_time']);
        if(isset($getData['user_phone']) && $getData['user_phone']) $sql = $sql->whereLike('user_phone','%'.$getData['user_phone'].'%');
        $count = $sql->count();
        $statusPay = [0=>'未支付',1=>'已支付'];
        $statusList = config('add.order_status_list');
        $stationModel = new ChargingStation();
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($statusPay,$statusList,$stationModel){
            $item['pay_name'] = $statusPay[$item['paid']];
            $item['status_name'] = $statusList[$item['status']];

            return $item;
        })->toArray();
        return compact('count','data');
    }



}
